//
//  LGAdviceOtherCell.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/10/7.
//

#import "LGAdviceRecordOtherCell.h"
#import "MCPlayer.h"
@interface LGAdviceRecordOtherCell()<MCPlayerDelegate>
/**   */
@property (nonatomic , strong)UIView *baseView;
/**   */
@property (nonatomic , strong)UILabel *titleLabel;
/**   */
@property (nonatomic , strong)UIView *recordView;
/**   */
@property (nonatomic , strong)NSMutableArray *buttonArray;
@end

@implementation LGAdviceRecordOtherCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubView];
    }
    return  self;
}

-(void)setType:(NSString *)type{
    _type = type;
    if([type isEqualToString:@"top"]){
        //第一个
        self.baseView.cornerRidus = 15;
        [self.titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentView).offset(viewPix(16));
        }];
        [self.recordView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.contentView).offset(-viewPix(6));
        }];
        [self.baseView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentView).offset(viewPix(4));
            make.bottom.equalTo(self.contentView).offset(viewPix(16));
        }];
    }else if ([type isEqualToString:@"middle"]){
        //中间
        self.baseView.cornerRidus = 0;
        [self.titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentView).offset(viewPix(6));
            make.bottom.equalTo(self.contentView).offset(-viewPix(10));
        }];
        [self.recordView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.contentView).offset(-viewPix(6));
        }];
        [self.baseView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentView).offset(-viewPix(16));
            make.bottom.equalTo(self.contentView).offset(viewPix(16));
        }];
    }else if ([type isEqualToString:@"bottom"]){
        //底部
        self.baseView.cornerRidus = 15;
        [self.titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentView).offset(viewPix(6));
        }];
        [self.recordView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.contentView).offset(-viewPix(16));
        }];
        [self.baseView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentView).offset(-viewPix(16));
            make.bottom.equalTo(self.contentView).offset(-viewPix(4));
        }];
    }else{
        self.baseView.cornerRidus = 15;
        [self.titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentView).offset(viewPix(16));
        }];
        [self.recordView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.contentView).offset(-viewPix(16));
        }];
        [self.baseView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentView).offset(viewPix(4));
            make.bottom.equalTo(self.contentView).offset(-viewPix(4));
        }];
    }
}


-(void)setDataDic:(NSDictionary *)dataDic{
    _dataDic = dataDic;
    NSString *content = LGNSString(dataDic[@"suggest"]);
    self.titleLabel.text = content;
    NSArray *recordArray = dataDic[@"voices"];
    CGFloat recordTop = (content.length>0)?0:-viewPix(12);
    CGFloat height = viewPix(44)*recordArray.count;
    [self.recordView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).offset(recordTop);
        make.height.equalTo(@(height));
    }];
    
    for (UIButton *button in self.buttonArray) {
        [button removeFromSuperview];
    }
    [self.buttonArray removeAllObjects];
    for (NSInteger i=0; i<recordArray.count; i++) {
        NSDictionary *tempDic = recordArray[i];
        UIButton *button = [UIButton buttonWithTitle:[NSString stringWithFormat:@"%@”",tempDic[@"duration"]] titleFont:LGFont(14) textColor:@"#FFFFFF" imageName:@"处理意见-播放录音"];
        button.frame = CGRectMake(0, viewPix(12)+viewPix(44)*i, viewPix(173), viewPix(32));
        button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        button.imageEdgeInsets = UIEdgeInsetsMake(0, viewPix(10), 0, 0);
        button.titleEdgeInsets = UIEdgeInsetsMake(0, viewPix(12), 0, 0);
        button.backgroundColor = [UIColor colorWithString:@"#0199FE"];
        button.cornerRidus = 4;
        button.tag = 1000+i;
        [button addTarget:self action:@selector(playRecordAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.recordView addSubview:button];
        [self.buttonArray addObject:button];
    }
    
}

//播放录音
-(void)playRecordAction:(UIButton *)sender{
    NSInteger index = sender.tag - 1000;
    NSArray *recordArray = self.dataDic[@"voices"];
    NSString *url = LGNSString(recordArray[index][@"file_url"]);
    MCPlayer *player = [MCPlayer shareManager];
    [player playMediaWithUrl:url tempPath:nil desPath:nil delegate:self];
}
-(void)playerPlayTimeSecond:(CGFloat)seconds duration:(CGFloat)duration currentStr:(NSString *)currentString withResidueStr:(NSString *)residueStr player:(NSObject *)player{
    if(seconds == duration){
        //播放完成
//        [self.currentBtn.imageView stopAnimating];
    }
}
#pragma mark
#pragma mark ====> 创建控件
-(void)createSubView{
    self.clipsToBounds = YES;
    self.backgroundColor = [UIColor clearColor];
    self.contentView.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:self.baseView];
    [self.contentView addSubview:self.titleLabel];
    [self.contentView addSubview:self.recordView];
    [self.baseView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(viewPix(16));
        make.right.equalTo(self.contentView).offset(-viewPix(16));
        make.top.equalTo(self.contentView).offset(viewPix(4));
        make.bottom.equalTo(self.contentView).offset(viewPix(4));
    }];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(viewPix(20));
        make.left.equalTo(self.contentView).offset(viewPix(28));
        make.right.equalTo(self.contentView).offset(-viewPix(28));
    }];
    [self.recordView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).offset(0);
        make.left.right.equalTo(self.titleLabel);
        make.bottom.equalTo(self.contentView).offset(-viewPix(6));
        make.height.equalTo(@(0));
    }];
}
-(UIView *)baseView{
    if(!_baseView){
        _baseView = [[UIView alloc] init];
        _baseView.backgroundColor = [UIColor whiteColor];
    }
    return _baseView;
}

-(UILabel *)titleLabel{
    if(!_titleLabel){
        _titleLabel = [UILabel lableWithText:@"" colorString:@"#333333" textFont:LGFont(14) textAlignment:NSTextAlignmentLeft lines:0];
    }
    return _titleLabel;
}

-(UIView *)recordView{
    if(!_recordView){
        _recordView = [[UIView alloc] init];
    }
    return _recordView;
}

@end
